﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DentScan2.DB;
using AjaxControlToolkit;

namespace DentScan2.WebApp.UC
{

    public partial class ucVehicles : Classes.BaseUserControl
    {
        DLDataContext dl = new DLDataContext();

        public Guid? MakeId
        {
            get
            {
                return ddlMake.SelectedValue == "" ? null : Guid.Parse(ddlMake.SelectedValue) as Guid?;
            }
            set
            {
                DisplayMakes(MakeId);
                ddlMake.SelectedValue = value.ToString();
                DisplayModels(null);
            }
        }

        public Guid? ModelId
        {
            get
            {
                return ddlModel.SelectedValue == "" ? null : Guid.Parse(ddlModel.SelectedValue) as Guid?;
            }
            set
            {
                ddlModel.SelectedValue = value.ToString();
            }
        }

        public Enums.Classificators Classificator
        {
            get
            {
                return (Enums.Classificators)ViewState["Classificator"];
            }
            set
            {
                ViewState["Classificator"] = value;
            }
        }

        public bool CanInsert
        {
            get;
            set;
        }

        public bool AddNull
        {
            get;
            set;
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetProperFilter(fteMake);
                SetProperFilter(fteModel);
                if (ddlMake.Items.Count == 0)
                    DisplayMakes(MakeId);
                if (ddlModel.Items.Count == 0)
                    DisplayModels(ModelId);
            }

        }

        

        private void DisplayMakes(Guid? id)
        {
            Tools.Tools.ClearDropDownSelection(ddlMake);
            Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
            ddlMake.DataTextField = "MakeName";
            ddlMake.DataValueField = "MakeId";
            ddlMake.DataSource = dl.Makes.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.MakeName);
            ddlMake.DataBind();
            if (AddNull)
                ddlMake.Items.Insert(0, new ListItem("", ""));
            if (id.HasValue)
                ddlMake.SelectedValue = id.Value.ToString();
            else
                if (ddlMake.Items.Count > 0)
                    ddlMake.Items[0].Selected = true;

            ibAddMake.Visible = CanInsert;

        }

        private void DisplayModels(Guid? id)
        {
            if (ddlMake.Items.Count > 0)
            {
                Tools.Tools.ClearDropDownSelection(ddlModel);
                Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
                if (ddlMake.SelectedValue != "")
                {
                    ddlModel.DataTextField = "ModelName";
                    ddlModel.DataValueField = "ModelId";
                    ddlModel.DataSource = dl.Models.Where(item => !item.IsDel && item.AreaId == areaId && item.MakeId == Guid.Parse(ddlMake.SelectedValue)).OrderBy(item => item.ModelName);
                    ddlModel.DataBind();
                }
                if (AddNull)
                    ddlModel.Items.Insert(0, new ListItem("", ""));

                if (id.HasValue)
                    ddlModel.SelectedValue = id.Value.ToString();
                else
                    if (ddlModel.Items.Count > 0)
                        ddlModel.Items[0].Selected = true;
            }
            else
                ddlModel.Items.Clear();

            ibAddModel.Visible = CanInsert && ddlMake.Items.Count > 0;

        }

        private void InsertMake()
        {
            tbMake.Text = "";
            tbMake.Visible = true;
            bSaveMake.Visible = true;
            bCancelMake.Visible = true;
            ddlMake.Visible = false;
            ibAddMake.Visible = false;
            tbMake.Focus();
            //mv.SetActiveView(vNew);
        }

        private void InsertModel()
        {
            tbModel.Text = "";
            tbModel.Visible = true;
            bSaveModel.Visible = true;
            bCancelModel.Visible = true;
            ddlModel.Visible = false;
            ibAddModel.Visible = false;
            tbModel.Focus();
            //mv.SetActiveView(vNew);
        }

        private void CancelMake()
        {
            tbMake.Visible = false;
            ddlMake.Visible = true;
            ibAddMake.Visible = true;
            bSaveMake.Visible = false;
            bCancelMake.Visible = false;
            //mv.SetActiveView(vShow);
        }

        private void CancelModel()
        {
            tbModel.Visible = false;
            ddlModel.Visible = true;
            ibAddModel.Visible = true;
            bSaveModel.Visible = false;
            bCancelModel.Visible = false;
            //mv.SetActiveView(vShow);
        }

        //private void Save()
        //{
        //    Page.Validate("vgVehicle");
        //    if (!Page.IsValid)
        //        return;

        //    Guid id = Guid.NewGuid();
        //    switch (this.Classificator)
        //    {

        //        case Enums.Classificators.Makes:
        //            DB.Make make = new Make()
        //            {
        //                MakeId = id,
        //                MakeName = tb.Text,
        //                IsDel = false,
        //                LastEditDate = DateTime.UtcNow,
        //                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
        //            };
        //            dl.Makes.InsertOnSubmit(make);
        //            dl.SubmitChanges();
        //            DisplayMakes(id);
        //            DisplayModels(null);
        //            break;
        //        case Enums.Classificators.Models:
        //            DB.Model model = new Model()
        //            {
        //                ModelId = id,
        //                ModelName = tb.Text,
        //                MakeId = Guid.Parse(ddlMake.SelectedValue),
        //                IsDel = false,
        //                LastEditDate = DateTime.UtcNow,
        //                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
        //            };
        //            dl.Models.InsertOnSubmit(model);
        //            dl.SubmitChanges();
        //            DisplayModels(id);
        //            break;
        //    }
        //    mv.SetActiveView(vShow);
        //}

        protected void bSaveMake_Click(object sender, ImageClickEventArgs e)
        {
            Page.Validate("vgMake");
            if (!Page.IsValid)
                return;
            Guid id = Guid.NewGuid();
            DB.Make make = new Make()
            {
                MakeId = id,
                MakeName = tbMake.Text.ToUpper(),
                IsDel = false,
                LastEditDate = DateTime.UtcNow,
                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
            };
            dl.Makes.InsertOnSubmit(make);
            dl.SubmitChanges();

            // връща елементите в начално състояние
            CancelMake();

            DisplayMakes(id);
            DisplayModels(null);
        }

        protected void bSaveModel_Click(object sender, ImageClickEventArgs e)
        {
            Page.Validate("vgModel");
            if (!Page.IsValid)
                return;
            Guid id = Guid.NewGuid();
            DB.Model model = new Model()
            {
                ModelId = id,
                ModelName = tbModel.Text.ToUpper(),
                MakeId = Guid.Parse(ddlMake.SelectedValue),
                IsDel = false,
                LastEditDate = DateTime.UtcNow,
                AreaId = (this.Page as Classes.BaseForm).AreaId.Value
            };
            dl.Models.InsertOnSubmit(model);
            dl.SubmitChanges();

            // връща елементите в начално състояние
            CancelModel();

            DisplayModels(id);
        }

        protected void bCancelMake_Click(object sender, ImageClickEventArgs e)
        {
            CancelMake();
        }

        protected void bCancelModel_Click(object sender, ImageClickEventArgs e)
        {
            CancelModel();
        }

        protected void ddlMake_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayModels(null);
        }

        protected void ibAddMake_Click(object sender, ImageClickEventArgs e)
        {
            this.Classificator = Enums.Classificators.Makes;
            InsertMake();
        }

        protected void ibAddModel_Click(object sender, ImageClickEventArgs e)
        {
            this.Classificator = Enums.Classificators.Models;
            InsertModel();
        }
    }
}